<?php
class Con_exp extends Acan{
	public $defaultAct='list';
	public $Conread = '快号';
	public $Actread = array('manage'=>'管理','list'=>'购买列表','chk'=>'检查扫描','buy'=>'购买快号','post'=>'发布快号');
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		$this->atitle = $this->Actread[$this->Aact];
		if($this->Aact!='post'){
			$this->danhao_sale_fun = Afunc_option('Acan_danhao_sale_fun')->get();
			$this->Appname = Afunc_option('Acan_danhao_sale_fun')->get();
		}
		$this->model = Afun_db()->model(Acan_table_danhao_express);
		$this->enum = array(
			'com'=>$this->model->enum['com'],
			'from'=>$this->model->enum['from'],
			'scan'=>$this->model->enum['scan'],
			'scanrr'=>array('全部','是','否'),
			'price'=>array('1'=>'0.5','2'=>'1'),
			'status'=>$this->model->enum['status'],
			'cash'=>array('1'=>'财务','2'=>'购买快号','3'=>'卖出快号','4'=>'申诉退回','5'=>'举报扣除','21'=>'充值','22'=>'提现'),
			'reason'=>array('1'=>'单号不存在','2'=>'发货地址不符','3'=>'收货地址不符','4'=>'扫描时间过早','5'=>'单号已被使用','6'=>'晚于揽收时间','7'=>'其他原因'),
		);
	}
	function Act_manage(){
		$obj = Afun_acan_new('list');
		$columnrr = $this->model->comment;//初始栏目
		unset($columnrr['uid']);
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$key_name = $this->model->key_name;
		$columnrr = $this->model->comment;//初始栏目
		$this->area = Afun_new('exp')->area();
		$this->model->enum['f1']=$this->model->enum['s1']=$this->area['province'];
		$this->model->enum['f2']=$this->model->enum['s2']=$this->area['cityr'];
		$this->model->enum['f3']=$this->model->enum['s3']=$this->area['distr'];
		$where = $this->search();
		if(!$_GET['start_date']) $_GET['start_date']= Afun_time_date(0,'Y-m-d').' 00';
		if(!$_GET['end_date']) $_GET['end_date'] = Afun_time_date(Afun_time_ex(time(),1,'d'),'Y-m-d').' 00';
		$con->where = "a.status=1 ".$where;
		$con->select = "a.*";
		$con->limit = 5000;
		$page->theurl = $this->Aactr.'-'.$this->id;
		if(!empty($_GET['exp'])){
			$con->where = "a.exp='".$_GET['exp']."'";
			$page->by = 'exp';
		}elseif($this->id>0){
			$con->where = "a.id=".$this->id;
			$page->by = 'id';
		}else{
			$page->by = 'send_date';
			$page->order = 'DESC';
		}
		$page->page = '20';
		if($_GET['apage']>200){$_GET['apage'] = 200;}
		$cache_name = 'page'.$_SERVER['REQUEST_URI'];
		$page->counts = Afun_cache_get($cache_name);
		if(!$page->counts){
			if ($this->_SS['uid']>0) $page->counts = $this->model->count($con);	//统计总数目
			if($page->counts>1000){Afun_cache_set($cache_name,$page->counts,600);}
		}
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->findAll($con);
		include $this->Atpl();
	}
	function Act_list(){
		$this->atitle = '购买快号';
		$this->area = Afun_new('exp')->area();
		$where = $this->search();
		if(!$_GET['start_date']) $_GET['start_date']= Afun_time_date(0,'Y-m-d').' 00';
		if(!$_GET['end_date']) $_GET['end_date'] = Afun_time_date(Afun_time_ex(time(),1,'d'),'Y-m-d').' 00';
		$con->where = "status=1 ".$where;
		$con->select = "a.*";
		$con->limit = 5000;
		$page->theurl = $this->Aactr.'-'.$this->id;
		//$page->by = $this->model->key_name;
		$page->by = 'send_date';
		$page->page = '20';
		$page->order = 'DESC';
		if($_GET['apage']>200){$_GET['apage'] = 200;}
		$cache_name = 'page'.$_SERVER['REQUEST_URI'];
		$page->counts = Afun_cache_get($cache_name);
		if(!$page->counts){
			if ($this->_SS['uid']>0) $page->counts = $this->model->count($con);	//统计总数目
			if($page->counts>1000){Afun_cache_set($cache_name,$page->counts,600);}
		}
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->findAll($con);
		/*
		if($this->_SS['group']==20 || $this->_SS['uid']==1){
			if($_GET['f1']>0){
				$con->limit='1000';
				$all = $this->model->findAll($con);
				foreach ($all as $k => $val) {
					$allstr .= '单号:'.$val['exp'].',快递类型:'.$this->enum['com'][$val['com']].',预计发货时间:'.$val['send_date'].',是否扫描:'.$this->enum['scan'][$val['scan']].',发货地址:'.$this->area['province'][$val['f1']].' '.$this->area['cityr'][$val['f2']].' '.$this->area['distr'][$val['f3']].',收货地址:'.$this->area['province'][$val['s1']].' '.$this->area['cityr'][$val['s2']].' '.$this->area['distr'][$val['s3']].';'."\n";
				}
			}
		}*/
		include $this->Atpl();
	}
	//检查是否扫描
	function Act_chk($id='',$scan=2){
		$rt = false;
		if(!empty($this->id)){$id = $this->id;}else{$rt = true;}
		if(!empty($_GET['scan'])){$scan = $_GET['scan'];}
		if(empty($id)){
			$rs = array('status'=>0,'msg'=>'请求参数错误');
		}else{
			$ebj = $this->model->findPk($id);
			$exp = $ebj['exp'];
			$com = $ebj['com'];
			$scan = $ebj['scan'];
			$url = "http://m.kuaidi100.com/query?type=&postid=&id=1&valicode=&temp=0.";
			$url_find = str_replace(array('type=','postid='),array('type='.$com,'postid='.$exp),$url).rand(100000,999999).time();
			$data = Afun_curl('get',$url_find);
			$json = json_decode($data['data']);
			if(!$json){
				$json = $data['data'];
			}else{
				//未扫描转已扫描
				unset($json->nu);
				if($json->status=='200' && $scan==2){
					$this->model->update(array('scan'=>1),array('exp'=>$exp));
				}
			}
			$rs['data'] = $json;
			$rs['status']=1;
		}
		if($rt){return $rs;}else{Afun_json($rs);}
	}
	function chk_exp($exp){
		if(Afun_cache_get('exp_'.$exp)==1){
			return false;
		}else{
			return true;
		}
	}
	//购买单号
	function buy_one($id){
		$price=0;$prr = array('1'=>50,'2'=>100);
		//锁定单号
		$rs = $this->model->update(array('status'=>3),array('id'=>$id,'status'=>1));
		if($rs==1){
			$obj = $this->model->findPk($id);
			$price = $prr[$obj['scan']];
			$crs = Afun_new("cash")->cash_minus($this->_SS['uid'],$price/100,'free',2,$obj['exp'],$obj['id']);
			if($crs>0){
				//扣款成功 增加卖家余额
				Afun_new("cash")->cash_add($obj['uid'],($price*0.8)/100,'money',3,$obj['exp'],$obj['id']);
			}else{
				return $crs;//余额不足
			}
		}elseif($rs==0){
			return 0;//单号不存在
		}
		//完成购买
		return $this->model->updatePk(array('status'=>2),$id);
	}
	function Act_buy(){
		$exps = $_POST['exp'];
		$tatol = count($exps);
		$i=0;
		if($tatol>0){
			foreach($exps as $k => $id){
				$rs = $this->buy_one($id);
				if($rs==-1){
					$status=3;
					$msg = '余额不足 ';
					break;
				}elseif($rs==0){
					$msg = '部分快号已经被卖出';
				}elseif($rs==1){
					$i++;
				}
			}
		}else{
			$msg = '请先选择要购买的快号';
		}
		if($tatol==$i){
			$msg = '购买成功';
			$status=1;
		}else if($i>0){
			$status=2;
			$msg .= ',仅成功购买'.$i.'个';
		}else if($i==0){
			$status=0;
		}
		if($i>0){
			Afunc_user()->load_info($this->_SS['uid']);
		}
		Afun_json(array('status'=>$status,'msg'=>$msg));
	}
	//发布数据提交
	function Act_post(){
		$city1 = array(1,2,9,22,33,34,35,36);
		$this->area = Afun_new('exp')->area();
		$err = false;
		if(in_array(Afun_get_ip(),array('112.124.9.158','127.0.0.1'))){
			header('Content-type: text/html; charset=UTF-8');
			header('Access-Control-Allow-Origin: *');
			$save['uid'] = 1;
		}else{
			$save['uid'] = $this->_SS['uid'];
		}
		$arr = $_POST;
		if($arr['exp'] && strlen($arr['exp'])>6){
			$save['exp'] = $arr['exp'];
		}else{$code = 'exp';$err = true;}
		//单号已发
		if(!$err && $this->model->find("send_date>='".Afun_time_date(Afun_time_ex(time(),-2,'d'),'Y-m-d')." 00:00:00' and exp='".$arr['exp']."'")){
			if(!$err) $code = '已经发布';$err = true;
		}
		//if(!$err && Afun_cache_get('exp_'.$arr['exp'])==1){if(!$err) $code = '已经发布';$err = true;}
		if(!$err && $arr['scan']>0 && $arr['scan']<3){
			$save['scan'] = $arr['scan'];
		}else{if(!$err) $code = 'scan';$err = true;}
		if(!$err && $arr['com']!="0" && $this->enum['com'][$arr['com']]){
			$save['com'] = $arr['com'];
		}else{if(!$err) $code = 'com';$err = true;}
		if(!$err && $arr['from']>0 && $this->enum['from'][$arr['from']]){
			$save['from'] = $arr['from'];
		}else{if(!$err) $code = 'from';$err = true;}
		if(!$err && $arr['f1'] && $this->area['province'][$arr['f1']]){
			$save['f1'] = $arr['f1'];
		}else{if(!$err) $code = 'f1';$err = true;}
		if(!$err && !empty($arr['f2']) && $this->area['city'][$save['f1']][$arr['f2']]){
			$save['f2'] = $arr['f2'];
		}else{if(!$err) $code = 'f2';$err = true;}
		if(!in_array($arr['f1'],$city1)){
			if(!$err && !empty($arr['f3']) && $this->area['dist'][$save['f2']][$arr['f3']]){
				$save['f3'] = $arr['f3'];
			}else{if(!$err) $code = 'f3';$err = true;}
		}
		if(!$err && $arr['s1'] && $this->area['province'][$arr['s1']]){
			$save['s1'] = $arr['s1'];
		}else{if(!$err) $code = 's1';$err = true;}
		if(!$err && !empty($arr['s2']) && $this->area['city'][$save['s1']][$arr['s2']]){
			$save['s2'] = $arr['s2'];
		}else{if(!$err) $code = 's2';$err = true;}
		if(!in_array($arr['s1'],$city1)){
			if(!$err && !empty($arr['s3']) && $this->area['dist'][$save['s2']][$arr['s3']]){
				$save['s3'] = $arr['s3'];
			}else{if(!$err) $code = 's3';$err = true;}
		}
		if(!$err && strlen($arr['send_date'])>10){
			$save['send_date'] = $arr['send_date'];
		}else{if(!$err) $code = 'send_date';$err = true;}
		if(!$err){
			$save['update'] = Afun_get_time();
			$rs = $this->model->save($save);
			if($rs==1){
				$status = 1;
				$msg = '发布成功';
				Afun_cache_set('exp_'.$arr['exp'],'1',86400*3);
			}else{
				$status = 2;
				$msg = '快号已经存在';
			}
			Afun_json(array('status'=>$status,'msg'=>$msg,'rs'=>$rs));
		}else{
			Afun_json(array('status'=>0,'msg'=>'格式错误','code'=>$code));
		}
	}
	function Act_posts(){
		
	}
	function search(){
		if($_GET){
			$Aget = $_GET;
			if($Aget['start_date']){
				$where .= " and a.send_date>='".$Aget['start_date']."'";
			}
			if($Aget['end_date']){
				$where .= " and a.send_date<='".$Aget['end_date']."'";
			}
			if($Aget['com']){
				$where .= " and a.com='".$Aget['com']."'";
			}
			if($Aget['from']){
				$where .= " and a.from=".$Aget['from']."";
			}
			if($Aget['scan']){
				$where .= " and a.scan=".$Aget['scan']."";
			}
			if(!empty($Aget['f1']) && is_numeric($Aget['f1'])){
				$where .= " and a.f1=".$Aget['f1']."";
				if(!empty($Aget['f2']) && is_numeric($Aget['f2'])){
					$where .= " and a.f2=".$Aget['f2']."";
					if(!empty($Aget['f3']) && is_numeric($Aget['f3'])){
						$where .= " and a.f3=".$Aget['f3']."";
					}
				}
			}
			if(!empty($Aget['s1']) && is_numeric($Aget['s1'])){
				$where .= " and a.s1=".$Aget['s1']."";
				if(!empty($Aget['s2']) && is_numeric($Aget['s2'])){
					$where .= " and a.s2=".$Aget['s2']."";
					if(!empty($Aget['s3']) && is_numeric($Aget['s3'])){
						$where .= " and a.s3=".$Aget['s3']."";
					}
				}
			}
		}
		return $where;
	}
}

?>